package org.grayrabbit.cms.action.common;

import java.util.Calendar;
import java.util.Timer;

import javax.servlet.ServletContextEvent;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.cms.constant.ConfigCode;
import org.grayrabbit.cms.content.base.ExportFileTask;
import org.grayrabbit.cms.entity.Config;
import org.grayrabbit.cms.service.lucene.impl.IndexTaskUpdate;
import org.grayrabbit.cms.service.system.ConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class TimeListener implements javax.servlet.ServletContextListener{

	private static final Logger log = LoggerFactory
			.getLogger(TimeListener.class);

	
	private ConfigService configService;
	private IndexTaskUpdate itu;
	private TimePublishTask tpt;
	public void init() {
		log.info("启动周期发布定时器");
		Config timeConfig = configService.getConfigByAttrKey(ConfigCode.PUBLISH_CYCLE);
		Config luceneTime = configService.getConfigByAttrKey(ConfigCode.LUCENE_UPDARE_INDEX);
		log.info("定时器时间:{}",timeConfig==null?null:timeConfig.getConfigAttrValue());
		try {
			Calendar cpublish = Calendar.getInstance();
			cpublish.setTimeInMillis(System.currentTimeMillis());
			int nowHour = cpublish.get(Calendar.HOUR_OF_DAY);
			if(nowHour>=2){
				cpublish.set(Calendar.HOUR_OF_DAY, 26);
			}
			else{
				cpublish.set(Calendar.HOUR, 2);
			}
			cpublish.set(Calendar.MINUTE, 15);
			cpublish.set(Calendar.SECOND, 0);
			//存在设置
			if(timeConfig!=null&&StringUtils.isNotBlank(timeConfig.getConfigAttrValue())){
				int hour = Integer.parseInt(timeConfig.getConfigAttrValue().split(":")[0]);
				cpublish.set(Calendar.MINUTE, Integer.parseInt(timeConfig.getConfigAttrValue().split(":")[1]));
				if(nowHour>hour){
					hour+=24;
				}
				cpublish.set(Calendar.HOUR_OF_DAY,hour);
			}
			tpt.addSchedule(cpublish.getTime());
			log.info("启动周期定时器启动成功");
			
			Calendar cindex = Calendar.getInstance();
			cindex.setTimeInMillis(System.currentTimeMillis());
			//默认23：20开始
			cindex.set(Calendar.HOUR_OF_DAY, 23);
			cindex.set(Calendar.MINUTE, 15);
			cindex.set(Calendar.SECOND, 0);
			//配置索引时间
			if(luceneTime!=null&&StringUtils.isNotBlank(luceneTime.getConfigAttrValue())){
				log.info("定时索引设置时间:{}",luceneTime.getConfigAttrValue());
				int hour = Integer.parseInt(luceneTime.getConfigAttrValue().split(":")[0]);
				if(nowHour>=hour){
					hour+=24;
				}
				cindex.set(Calendar.HOUR_OF_DAY,hour);
				cindex.set(Calendar.MINUTE, Integer.parseInt(luceneTime.getConfigAttrValue().split(":")[1]));
			}
			//启动定时索引
			itu.start(cindex.getTime());	
			log.debug("建立定时器成功.....,时间:{}",cindex.getTime());
			//启动索引程序
			itu.createAll();
		} catch (Exception e) {
			log.info("启动周期发布失败，异常：{}",e);
		}
	}

	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	public void contextInitialized(ServletContextEvent arg0) {
		configService = (ConfigService)SpringUtil.getBean("configServiceImpl");
		itu = (IndexTaskUpdate)SpringUtil.getBean("indexTaskUpdate");
		tpt = (TimePublishTask)SpringUtil.getBean("timePublishTask");
		init();
		deleteDiableFiles();
	}
	
	/**  定时删除 导出内容的压缩包 */
	private void deleteDiableFiles(){
		Timer timer = new Timer();
		timer.schedule(new ExportFileTask(), 60, ExportFileTask.TIME_SPACE);
	}
	
}
